package com.tjx.controller;


import com.tjx.common.Result;
import com.tjx.entity.Employee;
import com.tjx.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author tjx
 * @since 2025-10-29
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 管理员登录
     */
    @RequestMapping("/loginManagement")
    public Result<Employee> getById(@RequestBody Employee employee) {
        Employee selectEmployee = employeeService.loginManagement(employee);
        if (selectEmployee.getAccount() == null) {
            return Result.fail("账号错误", selectEmployee);
        }
        if (selectEmployee.getPassword() == null) {
            return Result.fail("密码错误", selectEmployee);
        }
        return Result.ok(selectEmployee);
    }

    /**
     * 新增员工
     */
    @RequestMapping("/employeeAdd")
    public Result<Employee> employeeAdd(@RequestBody Employee employee) {
        Integer row = employeeService.employeeAdd(employee);
        if (row == 0) {
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * 修改员工信息
     */
    @RequestMapping("/employeeUpdate")
    public Result<Employee> employeeUpdate(@RequestBody Employee employee) {
        Integer row = employeeService.employeeUpdate(employee);
        if (row == 0) {
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * 查询所有员工
     */
    @RequestMapping("/employeeQueryAll")
    public Result<List<Employee>> employeeQueryAll() {
        List<Employee> employeeList = employeeService.employeeQueryAll();
        if (employeeList.isEmpty()) {
            return Result.fail("查询失败", employeeList);
        }
        return Result.ok(employeeList);
    }
}
